﻿package nanoxml {
    import com.ericfeminella.collections.HashMap;

    import flash.xml.XMLDocument;
    import flash.xml.XMLNode;
    import flash.xml.XMLNodeType;

    public class XMLElement {
        private var _name:String;

        private var _chileds:Array = new Array;

        private var attributes:HashMap;

        private var _ignoreCase:Boolean = false;

        private var _contents:String = "";


        public function XMLElement() {
            attributes = new HashMap;

        }


        public function addChild(child:XMLElement):void {
            chileds[chileds.length] = child;
        }

        public function get name():String {
            return _name;
        }

        public function set name(value:String):void {
            _name = value;
        }

        public function get ignoreCase():Boolean {
            return _ignoreCase;
        }

        public function get chileds():Array {
            return _chileds;
        }

        public function get contents():String {
            return _contents;
        }

        public function set contents(value:String):void {
            _contents = value;
        }

        public function setAttribute(name:String, value:Object):void {
            if (this._ignoreCase) {
                name = name.toUpperCase();
            }
            this.attributes.put(name, value.toString());
        }

        public function setLongAttribute(name:String, value:Number):void {
            setAttribute(name, value);
        }

        public function setIntAttribute(name:String, value:Number):void {
            setAttribute(name, value);
        }

        public function getAttribute(name:String, defaultVal:Object = null):Object {
            if (this.ignoreCase) {
                name = name.toUpperCase();
            }
            var value:Object = this.attributes.getValue(name);
            if (value == null) {
                value = defaultVal;
            }
            return value;
        }

        public function getStringAttribute(name:String, defaultVal:String = ""):String {
            return getAttribute(name, defaultVal.toString()).toString();
        }

        public function getLongAttribute(name:String, defaultVal:Number = -1):Number {
            return parseInt(getStringAttribute(name, defaultVal.toString()));
        }

        public function getIntAttribute(name:String, defaultVal:int = -1):int {
            return parseInt(getStringAttribute(name, defaultVal.toString()));
        }

        public function toString(indent:Boolean = false):String {
//			var xDoc:XMLDocument = new XMLDocument();
//		  	xDoc.ignoreWhite = true;
//		  	xDoc.childNodes.;
            var s:String = getXML().toString();
            return s;
        }

        private function getXML():XMLNode {

            var xNode:XMLNode = new XMLNode(XMLNodeType.ELEMENT_NODE, contents);
            xNode.nodeName = name;
            if (_contents != null) {
                xNode.appendChild(new XMLNode(XMLNodeType.TEXT_NODE, _contents));
            }
            xNode.attributes = [];

            var attrKeys:Array = attributes.getKeys();
            for (var i:int = 0; i < attrKeys.length; i++) {
                var name:String = attrKeys[i];
                xNode.attributes[name] = attributes.getValue(name);
            }
            var len:int = chileds.length;
            for (var j:int = 0; j < len; j++) {
                xNode.appendChild(XMLElement(chileds[j]).getXML());
            }
            return xNode;
        }

        public function getContent():String {
            return this._contents;
        }

        public function setContent(content:String):void {
            this._contents = content;
        }

        public function parseString(str:String):void {

            var xDoc:XMLDocument = new XMLDocument();
            xDoc.ignoreWhite = true;
            xDoc.parseXML(str);
            var node:XMLNode = xDoc.firstChild;
            name = node.nodeName;
            var assoc:Object = node.attributes;
            attributes.clear();
            for (var i:String in assoc) {
                attributes.put(i, assoc[i]);
            }
            var childArr:Array = node.childNodes;
            var len:int = childArr.length;
            _chileds = [];
            for (var j:int = 0; j < len; j++) {

                var chNode:XMLNode = XMLNode(childArr[j]);
                if (chNode.nodeType == XMLNodeType.ELEMENT_NODE) {
                    var chEl:XMLElement = new XMLElement();
                    chEl.parseString(childArr[j].toString());
                    _chileds.push(chEl);
                } else {
                    if (chNode.nodeType == XMLNodeType.TEXT_NODE) {
                        _contents = chNode.nodeValue;
                    }
                }
            }

        }
    }
}

